<?php
/**
 * Manage jabber address book group data
 */
class Zend_Jabber_AddressBook_Group {
    /**
     * Group name
     *
     * @var string
     */
    private $name; 
    
    /**
     * Owner address book
     *
     * @var Zend_Jabber_AddressBook
     */
    private $addressBook;
    
    /**
     * Array of items (Zend_Jabber_AddressBook_Item)
     *
     * @var array
     */
    private $items;
    
    /**
     * Specifies if this group is default addressBook group
     * 
     * @var boolean
     */
    private $default = false;
    
    /**
     * Create new address book group
     * 
     * @param Zend_Jabber_AddressBook $addressBook Address book
     * @param string name Group name
     * @param boolead $default Is this group a default address book group
     */
    public function __construct(Zend_Jabber_AddressBook $addressBook, $name, $default = false) {
        $this->addressBook = $addressBook;
        $this->name = $name;
        $this->default = $default;
    }
    
    /**
     * Return true if this group is a default address book group
     * 
     * @return boolean
     */
    public function IsDefault() {
        return $this->default;
    }
    
    /**
     * Get group name
     * 
     * @return string
     */
    public function getName() {
        return $this->name;
    }
    
    /**
     * Get owner address book object
     *
     * @return Zend_Jabber_AddressBook
     */
    public function getAddressBook() {
        return $this->addressBook;
    }
    
    /**
     * Add new item to address book group
     * 
     * @param Zend_Jabber_AddressBook_Item
     */
    public function addItem(Zend_Jabber_AddressBook_Item $item) {
          $this->items[$item->getUser()->getJid(false)] = $item;
    }
    
    /**
     * Get array of items in address book group
     * 
     * @return array
     */
    public function getItems() {
        return $this->items;
    }
    
    /**
     * Remove address book item from address book group
     * 
     * @param Zend_Jabber_AddressBook_Item $item Roster item
     */
    public function removeItem(Zend_Jabber_AddressBook_Item $item) {
        unset($this->items[$item->getUser()->getJid(false)]); 
    }
    
    /**
     * Count items in current address book group
     * 
     * @return integer
     */
    public function countItems() {
        return count($this->getItems());
    }

    /**
     * Convert address book group to XML 
     *
     * @return DomDocument
     */
    public function toDom() {
        $dom = new DomDocument();
        $groupNode = $dom->createElement('group', $this->getName());
        $dom->appendChild($groupNode);
        
        return $dom;
    }
}
?>